from Crypto.Cipher import AES

from Crypto.Util.Padding import pad, unpad
from base64 import b64encode, b64decode

class Aes:
    VIPARA = "0102030405060708"
    bm = "GBK"

    @staticmethod
    def encrypt(cleartext, private_key):
        try:
            cipher = AES.new(private_key.encode(Aes.bm), AES.MODE_CBC, Aes.VIPARA.encode(Aes.bm))
            ct_bytes = cipher.encrypt(pad(cleartext.encode(Aes.bm), AES.block_size))
            ct = b64encode(ct_bytes).decode('utf-8')
            return ct
        except Exception as e:
            print(e)
            return None

    @staticmethod
    def decrypt(encrypted, private_key):
        try:
            ct = b64decode(encrypted)
            cipher = AES.new(private_key.encode(Aes.bm), AES.MODE_CBC, Aes.VIPARA.encode(Aes.bm))
            pt = unpad(cipher.decrypt(ct), AES.block_size)
            return pt.decode(Aes.bm)
        except Exception as e:
            print(e)
            return None